/*************************************************************************
  > File Name: main.c
  > Author: Happyfrog
  > Mail: yanghappyfrog@163.com
  > Created Time: 2017年11月01日 星期三 14时56分01秒
 ************************************************************************/

#include <stdio.h>
#include "libcard.h"

int main(int argc, char* argv[])
{
	pthread_mutex_t mutex;
	rf_card* card;
	sem_t sem;
	short delaytime = 250;
	char cardtype = 0;
	unsigned char UIDLen = 0;
	char cardUID[64];
	int i = 0;
	float money = 0.0;
	char locknum = 0;
	int ret  = 0;
	unsigned char sernum[4] = {0x01,0x02,0x03,0x04};
	yct_rf_consum_card card_info;
	yct_incomplete_trade record;
	unsigned char tac[4];
	unsigned char psam[6]={0x01,0x00,0x99,0x99,0x31,0x70};

	unsigned char devNum[8]={0x44,0x02,0x00,0x00,0x03,0x32,0x00,0x09};


	pthread_mutex_init(&mutex,NULL);
	sem_init(&sem,0,0);

	do{
		card = (rf_card*)malloc(sizeof(rf_card));
	}while(card == NULL);

	strcpy(card->com.name,"/dev/ttyO1");
	card->com.baud = 57600;
	card->com.data_bits = 8;
	card->com.parity = 0;
	card->com.stop_bits = 1;

	protocol_card_init(card,&mutex,&sem);
	protocol_yct_card_set_info(psam, devNum);
	sleep(5);
	ret = protocol_read_card(&money, &locknum);
	if (ret != 0){
		printf("读卡失败，返回值:%d\n",ret);
		return ret;
	}
	printf("读卡成功，返回值:%d\n",ret);
	//	int protocol_yct_card_preconsume(int org_money, int real_money, unsigned char sernum[4], yct_rf_consum_card* card_info);

	ret = protocol_yct_card_preconsume(10,0,&card_info);
	if(ret != 0 && ret !=  4){
		printf("预处理失败，返回值:%d\n",ret);
		return ret;
	}
	printf("预处理成功，返回值:%d\n",ret);
	record.balance = card_info.cur_balance;
	record.ticket_trade_count = card_info.consum_count;
	record.trade_money = 1;
	memcpy(record.cardNum,card_info.phy_card_Num,8);
	memcpy(record.logic_cardNum,card_info.logic_card_Num,8);
	protocol_read_card(&money, &locknum);
	sleep(3);


	//	int protocol_yct_card_settle_accounts(int org_money, int real_money,unsigned char sernum[4],yct_rf_consum_card* card_info);
#if 1
	ret = protocol_yct_card_settle_accounts(10,0,&card_info);
	if(ret != 0){
		printf("结算失败，返回值:%d\n",ret);
	}
	else
		printf("结算成功，返回值:%d\n",ret);

#else
	//	int protocol_yct_card_incomplete_settle(yct_incomplete_trade* record, unsigned char tac[4]);
	ret = protocol_yct_card_incomplete_settle(&record,tac);
	if(ret != 0){
		printf("未完整交易结算失败，返回值:%d\n",ret);
	}
	else
		printf("未完整交易结算成功，返回值:%d\n",ret);
#endif

	ret = protocol_read_card(&money, &locknum);


	sleep(5);

#if 0
	while(1)
	{
		ret = sleep(1);
		while(ret)
			ret = sleep(ret);

		ret = protocol_read_card(&money, &locknum);

		printf("\n############ Card Info ##################\n");

		printf("card.cardNUM = ");
		for(i=0; i<8; i++)
			printf("%02x",card->cardNUM[i]);
		printf("\n");
		printf("card.ver = %02x\n",card->ver);
		printf("card.money = %d\n",card->available_money);
		printf("card.locknum = %d\n",card->max_lockcount);
		printf("\n############    Card  ##################\n");

		if(ret == 1){
			printf("\n\n\n\n\n\n\n\n READ CARD ERROR\n\n\n\n\n");
		}

	}

#endif

	return 0;
}
